/* 
 * Copyright (C)2008 Vladimir Hozjan vladimir.hozjan@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

/**
 * @file  tetrisElement.cpp
 *
 * Implementation of tetris element
 *
 * @author Vladimir Hozjan
 * @date   30.12.2008 
 *
 * @remarks VHOZ [30.12.2008]: file created
 */
#include "tetrisElement.h"

/**
 * TetrisElement
 *
 * Constructor for element. Each element has shape dimension x dimension.
 *
 * @param[in] aDimension - dimension of shape
 * @param[in] aCells     - shape of element in cells
 */
TetrisElement::TetrisElement( unsigned short aElementId, unsigned short aDimension, const unsigned short **aCells ):
TetrisMatrix(aDimension, aDimension),
mElementId(aElementId)
{
  /* Copy all elements to allocated space*/
  int i, j;
  for (i = 0; i < mNumRows;  i++) {
    for (j = 0; j < mNumCols; j++) {
      if (aCells[i][j] != 0) {
        mMatrix[i][j] = mElementId;
      }
    }
  }
}

/**
 * TetrisElement
 *
 * Copy constructor for element.
 *
 * @param[in] TetrisElement
 */
TetrisElement::TetrisElement( TetrisElement const & aTetrisElement) :
TetrisMatrix(aTetrisElement)
{
  /* Copy all elements to allocated space*/
  int i, j;
  for (i = 0; i < mNumRows;  i++) {
    for (j = 0; j < mNumCols; j++) {
      mMatrix[i][j] = aTetrisElement.mMatrix[i][j];
    }
  }
  mElementId = aTetrisElement.mElementId;
}

/**
 * ~TetrisElement
 *
 * Destructor
 */
TetrisElement::~TetrisElement() {
}

/**
 * rotateElementLeft
 *
 * Rotate element to the left.
 */
void TetrisElement::rotateElementLeft() {
  /* Create temporary element*/
  TetrisElement tmp(*this);
  
  /* Copy all cells to tmp in correct way*/
  int i, j;
  for (i = 0; i < mNumRows;  i++) {
    for (j = 0; j < mNumCols; j++) {
      mMatrix[i][j] = tmp.mMatrix[mNumRows - j - 1][i];
    }
  }
}

/**
 * rotateElementRight
 *
 * Rotate element to the right.
 */
void TetrisElement::rotateElementRight() {
  /* Create temporary element*/
  TetrisElement tmp(*this);

  /* Copy all cells to tmp in correct way*/
  int i, j;
  for (i = 0; i < mNumRows;  i++) {
    for (j = 0; j < mNumCols; j++) {
      mMatrix[mNumRows - j - 1][i] = tmp.mMatrix[i][j];
    }
  }
}

/**
 * getDimension
 *
 * Method return dimension of element. Element must have number of rows
 * and column equal. If this two numbers are different, we assume error
 * occurred.
 *
 * @return Dimension of element. If error return 0.
 */
unsigned short TetrisElement::getDimension() const {
  unsigned short retval = 0;
  if (mNumCols == mNumRows) {
    retval = mNumRows;
  }
  return retval;
}
